package com.itmiaojie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itmiaojie.handle.exception.SystemException;
import com.itmiaojie.pojo.AppHttpCodeEnum;
import com.itmiaojie.pojo.ResponseResult;
import com.itmiaojie.pojo.User;
import com.itmiaojie.pojo.UserInfoVo;
import com.itmiaojie.service.UserService;
import com.itmiaojie.mapper.UserMapper;
import com.itmiaojie.utils.BeanCopyUtils;
import com.itmiaojie.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

/**
 *
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Override
    public ResponseResult userInfo() {
        //获取当前用户id
        Long userId = SecurityUtils.getUserId();
        //根据用户id查询用户信息
        User user = getById(userId);
        //封装成UserInfoVo
        UserInfoVo vo = BeanCopyUtils.copyBean(user,UserInfoVo.class);
        return ResponseResult.okResult(vo);
    }

    @Override
    public ResponseResult updateUserInfo(User user) {
        updateById(user);
        return ResponseResult.okResult();
    }
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Override
    public ResponseResult register(User user) {

            //对数据进行非空判断
            if(!StringUtils.hasText(user.getUserName())){
                throw new SystemException(AppHttpCodeEnum.USERNAME_NOT_NULL);
            }
            if(!StringUtils.hasText(user.getPassword())){
                throw new SystemException(AppHttpCodeEnum.PASSWORD_NOT_NULL);
            }

            if(!StringUtils.hasText(user.getNickName())){
                throw new SystemException(AppHttpCodeEnum.NICKNAME_NOT_NULL);
            }
            //对数据进行是否存在的判断
            if(!userNameExist(user.getUserName())){
                throw new SystemException(AppHttpCodeEnum.USERNAME_EXIST);
            }
            if(!nickNameExist(user.getNickName())){
                throw new SystemException(AppHttpCodeEnum.NICKNAME_EXIST);
            }
            //...
            //对密码进行加密
            String encodePassword = passwordEncoder.encode(user.getPassword());
            user.setPassword(encodePassword);
            //存入数据库
            save(user);
            return ResponseResult.okResult();
        }


    private boolean nickNameExist(String nickName) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getNickName,nickName);
        User user = baseMapper.selectOne(queryWrapper);
        return  user == null;
    }

    private boolean userNameExist(String userName) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUserName,userName);
        User user = baseMapper.selectOne(queryWrapper);
        System.out.println("user = " + user);
        return  user == null;
    }
}




